<?php

namespace iBrand\Shop\Core\Repositories;

use iBrand\Shop\Core\Models\User;
use iBrand\Shop\Core\Models\UserShop;
use Prettus\Repository\Eloquent\BaseRepository;
use Carbon\Carbon;
use Illuminate\Support\Str;

class UserShopRepository extends BaseRepository
{

    const OPEN = 1;
    const CLOSE = 0;

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return UserShop::class;
    }

    /**
     * 获取当前门店所有会员列表
     *
     * @param     $shop_id
     * @param int $limit
     *
     * @return mixed
     */
    public function getUserListByShopId($shop_id, $limit = 10)
    {
        return User::where('status', 1)
            ->whereHas('shopUser', function ($query) use ($shop_id) {
                return $query->where('shop_id', $shop_id);
            })->with('shopUser')
            ->orderBy('created_at', 'desc')
            ->paginate($limit);

        /*return $this->model->where('shop_id', $shop_id)
            ->whereNotNull('user_id')
            ->with('user')
            ->whereHas('user', function ($query) {
                return $query->where('status', 1);
            })
            ->orderBy('created_at', 'desc')
            ->paginate($limit);*/
    }

    /**
     * 根据手机号码获取门店会员
     *
     * @param     $mobile
     * @param     $shop_id
     * @param int $limit
     *
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getUserListByCredentials($mobile, $shop_id, $limit = 10)
    {
        if (is_mobile($mobile)) {
            $users = User::where('status', 1)
                ->where('mobile', $mobile)
                ->with('shopUser')
                ->orderBy('created_at', 'desc');
        } else {
            $users = User::where('status', 1)
                ->where('mobile', 'like', '%' . $mobile . '%')
                ->with('shopUser')
                ->orderBy('created_at', 'desc');
        }

        /*if (is_mobile($mobile)) {
            $users = $this->model->with('user')
                ->whereHas('user', function ($query) use ($mobile) {
                    return $query->where('mobile', $mobile);
                })->orderBy('created_at', 'desc');
        } else {
            $users = $this->model->where('shop_id', $shop_id)
                ->with('user')
                ->whereHas('user', function ($query) use ($mobile) {
                    return $query->where('mobile', 'like', '%' . $mobile . '%');
                })->orderBy('created_at', 'desc');
        }*/

        return $users->paginate($limit);
    }

}
